package all

import (
	"github.com/gin-gonic/gin"
	"ruoyi/application"
	"ruoyi/infrastructure/persistence"
	"ruoyi/interfaces"
	"ruoyi/interfaces/middleware"
)

func InitTest(e *gin.Engine, s *persistence.SystemRepositories, m *persistence.MonitorRepositories) {

	captch := interfaces.NewCaptcha(s.Config)
	e.GET("/captchaImage", captch.CaptchaImageHandler)

	login := interfaces.NewLoginService(s.User, s.Config, s.Log, s.Menu, s.Role, s.Dept)
	e.POST("/login", login.LoginHandler)

	v1 := e.Group("/")
	{
		auth := v1.Group("")
		//限流测试
		//auth.Use(utils.LimitHandler(100))
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("getInfo", login.GetInfoHandler)
			auth.POST("logout", login.LogoutHandler)
			/*获取用户授权菜单*/
			auth.GET("getRouters", login.GetRoutersHandler)
		}
	}

	//消息相关
	noticeGroup := New("/system/notice", middleware.JWTAuthMiddleware(), Auth)
	notice := interfaces.NewNotice(s.Notice, s.User)
	noticeGroup.GET("/list", "system:notice:list", notice.ListNotice)
	noticeGroup.GET("/:noticeId", "system:notice:query", notice.GetNotice)
	noticeGroup.POST("", "system:notice:add", notice.SaveNotice)
	noticeGroup.PUT("", "system:notice:edit", notice.UploadNotice)
	noticeGroup.DELETE("/:noticeIds", "system:notice:remove", notice.DeleteNotice)

	//字典相关
	dictGroup := New("/system/dict", middleware.JWTAuthMiddleware(), Auth)
	dict := interfaces.NewDict(s.Dict, s.User, s.DictType)
	dictGroup.GET("/data/list", "system:dict:list", dict.ListDict)
	dictGroup.POST("/data/export", "system:dict:export", dict.ExportDict)
	dictGroup.GET("/data/:dictCode", "system:dict:query", dict.GetDictCode)
	dictGroup.GET("/data/type/:dictType", "", dict.DictTypeHandler)
	dictGroup.POST("/data", "system:dict:add", dict.SaveDictData)
	dictGroup.PUT("/data", "system:dict:edit", dict.UpDictData)
	dictGroup.DELETE("/data/:dictCodes", "system:dict:remove", dict.DeleteDictData)
	dictGroup.GET("/type/list", "system:dict:list", dict.ListDictType)
	dictGroup.POST("/type/export", "system:dict:export", dict.ExportType)
	dictGroup.GET("/type/:dictId", "system:dict:query", dict.GetTypeDict)
	dictGroup.POST("/type", "system:dict:add", dict.SaveType)
	dictGroup.PUT("/type", "system:dict:edit", dict.UploadType)
	dictGroup.DELETE("/type/:dictIds", "system:dict:remove", dict.DeleteDataType)
	dictGroup.DELETE("/refreshCache", "system:dict:remove", dict.RefreshCache)
	dictGroup.GET("/type/optionselect", "", dict.GetOptionSelect)

	// 部门相关
	dept := interfaces.NewDept(s.Dept, s.User)
	deptGroup := New("/system/dept", middleware.JWTAuthMiddleware(), Auth)
	deptGroup.GET("/list", "system:dept:list", dept.ListDept)
	deptGroup.GET("/list/exclude/:deptId", "system:dept:list", dept.ExcludeDept)
	deptGroup.GET("/:deptId", "system:dept:query", dept.GetDept)
	deptGroup.POST("", "system:dept:add", dept.SaveDept)
	deptGroup.PUT("", "system:dept:edit", dept.UpDataDept)
	deptGroup.DELETE("/:deptId", "system:dept:remove", dept.DeleteDept)

	// 菜单相关
	menu := interfaces.NewMenu(s.Menu, s.User, s.Role)
	menuGroup := New("/system/menu", middleware.JWTAuthMiddleware(), Auth)
	menuGroup.GET("/list", "system:menu:list", menu.ListMenu)
	menuGroup.GET("/:menuId", "system:menu:query", menu.GetMenuInfo)
	menuGroup.GET("/treeselect", "", menu.GetTreeSelect)
	menuGroup.GET("/roleMenuTreeselect/:roleId", "", menu.TreeSelectByRole)
	menuGroup.POST("", "system:menu:add", menu.SaveMenu)
	menuGroup.PUT("", "system:menu:edit", menu.UploadMenu)
	menuGroup.DELETE("/:menuId", "system:menu:remove", menu.DeleteMenu)

	//岗位相关
	post := interfaces.NewPost(s.Post, s.User)
	postGroup := New("/system/post", middleware.JWTAuthMiddleware(), Auth)
	postGroup.GET("/list", "system:post:list", post.ListPost)
	postGroup.POST("/export", "system:post:export", post.ExportPost)
	postGroup.GET("/:postId", "system:post:query", post.GetPostInfo)
	postGroup.POST("", "system:post:add", post.SavePost)
	postGroup.PUT("", "system:post:edit", post.UploadPost)
	postGroup.DELETE("/:postIds", "system:post:remove", post.DeletePost)
	postGroup.GET("/optionselect", "", post.GetPostOptionSelect)

	//角色管理
	role := interfaces.NewRole(s.Role, s.User, s.Dept)
	roleGroup := New("/system/role", middleware.JWTAuthMiddleware(), Auth)
	roleGroup.GET("/list", "system:role:list", role.ListRole)
	roleGroup.POST("/export", "system:role:export", role.ExportRole)
	roleGroup.GET("/:roleId", "system:role:query", role.GetRoleInfo)
	roleGroup.POST("", "system:role:add", role.SaveRole)
	roleGroup.PUT("", "system:role:edit", role.UploadRole)
	roleGroup.PUT("/dataScope", "system:role:edit", role.PutDataScope)
	roleGroup.PUT("/changeStatus", "system:role:edit", role.ChangeRoleStatus)
	roleGroup.DELETE("/:roleIds", "system:role:remove", role.DeleteRole)
	roleGroup.GET("/optionselect", "system:role:query", role.GetRoleOptionSelect)
	roleGroup.GET("/authUser/allocatedList", "system:role:list", role.GetAllocatedList)
	roleGroup.GET("/authUser/unallocatedList", "system:role:list", role.GetUnAllocatedList)
	roleGroup.PUT("/authUser/cancel", "system:role:edit", role.CancelRole)
	roleGroup.PUT("/authUser/cancelAll", "system:role:edit", role.CancelAllRole)
	roleGroup.PUT("/authUser/selectAll", "system:role:edit", role.SelectRoleAll)
	roleGroup.GET("/deptTree/:roleId", "system:role:query", role.GetDeptTreeRole)

	//用户管理
	user := interfaces.NewUser(s.Role, s.User, s.Dept, s.Post)
	userGroup := New("/system/user", middleware.JWTAuthMiddleware(), Auth)
	userGroup.GET("/list", "system:user:list", user.ListUser)
	userGroup.POST("/export", "system:user:export", user.ExportExport)
	userGroup.POST("/importData", "system:user:import", user.ImportUserData)
	userGroup.POST("/importTemplate", "", user.ImportTemplate)
	userGroup.GET("/:userId", "system:user:query", user.GetUserInfo)
	userGroup.GET("/", "", user.GetUserInfo)
	userGroup.POST("", "system:user:add", user.SaveUser)
	userGroup.PUT("", "system:user:edit", user.UploadUser)
	userGroup.DELETE("/:userIds", "system:user:remove", user.DeleteUserById)
	userGroup.PUT("/resetPwd", "system:user:resetPwd", user.ResetPwd)
	userGroup.PUT("/changeStatus", "system:user:edit", user.ChangeUserStatus)
	userGroup.GET("/authRole/:userId", "system:user:query", user.GetAuthUserRole)
	userGroup.PUT("/authRole", "system:user:edit", user.PutAuthUser)
	userGroup.GET("/deptTree", "system:user:list", user.GetUserDeptTree)

	//个人用户相关
	personageGroup := New("/system/user/profile", middleware.JWTAuthMiddleware(), Auth)
	personageGroup.GET("", "", user.ProfileHandler)
	personageGroup.PUT("", "", user.PostProfileHandler)
	personageGroup.PUT("/updatePwd", "", user.UpdatePwdHandler)
	personageGroup.POST("/avatar", "", user.AvatarHandler)

	// 服务监控
	service := interfaces.NewService(&application.ServiceApp{}, m.Operation)
	serviceGroup := New("/monitor", middleware.JWTAuthMiddleware(), Auth)
	serviceGroup.GET("/server", "monitor:server:list", service.ServerData)

	// 操作日志相关
	operation := interfaces.NewOperationLog(m.Operation)
	operationGroup := New("/monitor/operlog", middleware.JWTAuthMiddleware(), Auth)
	operationGroup.GET("/list", "monitor:operlog:list", operation.ListOperationLog)
	operationGroup.DELETE("/:operId", "monitor:operlog:remove", operation.DelectOperationLog)
	operationGroup.DELETE("/clean", "monitor:operlog:remove", operation.ClearOperationLog)
	operationGroup.POST("/export", "monitor:operlog:export", operation.ExportOperationLog)

	// 定时任务相关
	job := interfaces.NewJob(m.Job)
	jobGroup := New("/monitor/job", middleware.JWTAuthMiddleware(), Auth)
	jobGroup.GET("list", "", job.ListJob)
	jobGroup.POST("export", "", job.ExportJob)
	jobGroup.GET(":jobId", "", job.GetJobById)
	jobGroup.POST("", "", job.SaveJob)
	jobGroup.PUT("", "", job.UploadJob)
	jobGroup.PUT("changeStatus", "", job.ChangeStatus)
	jobGroup.PUT("run", "", job.RunJob)
	jobGroup.DELETE(":jobIds", "", job.DeleteJob)

	// 在线用户相关
	online := interfaces.NewOnline(m.Operation)
	onlineGroup := New("/monitor/online", middleware.JWTAuthMiddleware(), Auth)
	onlineGroup.GET("list", "monitor:online:list", online.ListOnLine)
	onlineGroup.DELETE("/:tokenId", "monitor:online:forceLogout", online.DeleteOnLine)

	// 登录日志相关
	log := interfaces.NewLog(s.Log)
	logGroup := New("/monitor/logininfor", middleware.JWTAuthMiddleware(), Auth)
	logGroup.GET("/list", "monitor:logininfor:list", log.LoginInformListHandler)
	logGroup.POST("/export", "monitor:logininfor:export", log.ExportHandler)
	logGroup.DELETE("/:infoIds", "monitor:logininfor:remove", log.DeleteByIdHandler)
	logGroup.DELETE("/clean", "monitor:logininfor:remove", log.CleanHandler)
	logGroup.GET("/unlock/:userName", "monitor:logininfor:unlock", log.UnlockHandler)

	// 缓存相关
	cache := interfaces.NewCache()
	cacheGroup := New("/monitor/cache", middleware.JWTAuthMiddleware(), Auth)
	cacheGroup.GET("", "monitor:cache:list", cache.CacheHandler)
	cacheGroup.GET("getNames", "monitor:cache:list", cache.CacheHandler)
	cacheGroup.GET("getKeys/:cacheName", "monitor:cache:list", cache.GetCacheKeysHandler)
	cacheGroup.DELETE("clearCacheName/:cacheName", "", cache.ClearCacheNameHandler)
	cacheGroup.DELETE("clearCacheKey/:cacheKey", "", cache.ClearCacheKeyHandler)
	cacheGroup.DELETE("clearCacheAll", "monitor:cache:list", cache.ClearCacheAllHandler)

	//配置相关
	config := interfaces.NewConfig(s.Config, s.User)
	g1 := New("/system/config", middleware.JWTAuthMiddleware(), Auth)
	g1.GET("/list", "system:config:list", config.ListConfig)
	g1.POST("/export", "system:config:export", config.ExportConfig)
	g1.GET("/:configId", "system:config:query", config.GetConfigInfo)
	g1.GET("/configKey/:configKey", "", config.GetConfigKey)
	g1.POST("", "system:config:add", config.SaveConfig)
	g1.PUT("", "system:config:edit", config.UploadConfig)
	g1.DELETE("/:configIds", "system:config:remove", config.DetectConfig)
	g1.DELETE("/donws/:refreshCache", "system:config:remove", config.DeleteCacheConfig)

	routerBase := e.Group("")
	//注册路由
	if len(GroupList) > 0 {
		for _, group := range GroupList {
			grp := routerBase.Group(group.RelativePath, group.Handlers...)
			for _, r := range group.Router {
				if r.Method == "ANY" {
					grp.Any(r.RelativePath, r.HandlerFunc...)
				} else {
					grp.Handle(r.Method, r.RelativePath, r.HandlerFunc...)
				}
			}
		}
	}
}
